package edu.hcmus.sow.web;

import java.util.List;

import edu.hcmus.sow.dao.FileDAO;

import edu.hcmus.sow.domain.File;
import edu.hcmus.sow.service.FileService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

/**
 * Spring MVC controller that handles CRUD requests for File entities
 * 
 */

@Controller("FileController")
public class FileController {

   /**
    * DAO injected by Spring that manages File entities
    * 
    */
   @Autowired
   private FileDAO fileDAO;

   /**
    * Service injected by Spring that provides CRUD operations for File entities
    * 
    */
   @Autowired
   private FileService fileService;

   @RequestMapping("/welcome")
   public String printWelcome() {
      //model.addAttribute("message", "ABC");
      return "hello";
   }
   
   @RequestMapping("/findAll")
   public ModelAndView findAll() {
      ModelAndView mav = new ModelAndView();
      List<File> files = fileDAO.findAll();
      mav.addObject("files", files);
      mav.setViewName("hello");
      
      return mav;
   }

   /**
    * Delete an existing File entity
    * 
    */
   @RequestMapping("/deleteFile")
   public String deleteFile(@RequestParam Integer idKey) {
      File file = fileDAO.findById(idKey);
      fileService.deleteFile(file);
      return "forward:/indexFile";
   }

   /**
    * Edit an existing File entity
    * 
    */
   @RequestMapping("/editFile")
   public ModelAndView editFile(@RequestParam Integer idKey) {
      ModelAndView mav = new ModelAndView();

      mav.addObject("file", fileDAO.findById(idKey));
      mav.setViewName("file/editFile.jsp");

      return mav;
   }

   /**
    * Select an existing File entity
    * 
    */
   @RequestMapping("/selectFile")
   public ModelAndView selectFile(@RequestParam Integer idKey) {
      ModelAndView mav = new ModelAndView();

      mav.addObject("file", fileDAO.findById(idKey));
      mav.setViewName("file/viewFile.jsp");

      return mav;
   }

   /**
    * Save an existing File entity
    * 
    */
   @RequestMapping("/saveFile")
   public String saveFile(@ModelAttribute File file) {
      fileService.saveFile(file);
      return "forward:/indexFile";
   }

   /**
    * Create a new File entity
    * 
    */
   @RequestMapping("/newFile")
   public ModelAndView newFile() {
      ModelAndView mav = new ModelAndView();

      mav.addObject("file", new File());
      mav.addObject("newFlag", true);
      mav.setViewName("file/editFile.jsp");

      return mav;
   }

   /**
    * Entry point to show all File entities
    * 
    */
   public String indexFile() {
      return "redirect:/indexFile";
   }

   /**
    * Show all File entities
    * 
    */
   @RequestMapping("/indexFile")
   public ModelAndView listFiles() {
      ModelAndView mav = new ModelAndView();

      mav.addObject("files", fileService.findAll());

      mav.setViewName("file/listFiles.jsp");

      return mav;
   }

   /**
    * Select the File entity for display allowing the user to confirm that they
    * would like to delete the entity
    * 
    */
   @RequestMapping("/confirmDeleteFile")
   public ModelAndView confirmDeleteFile(@RequestParam Integer idKey) {
      ModelAndView mav = new ModelAndView();

      mav.addObject("file", fileDAO.findById(idKey));
      mav.setViewName("file/deleteFile.jsp");

      return mav;
   }

}